﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using CYTS.Aoyou.Framework.Config;

namespace CYTS.Aoyou.Framework.Service
{
    public class ServiceClientConfig : ConfigUnit
    {
        public List<ClientEndpoint> Client { get; set; }
    }


    public class ClientEndpoint
    {
        [XmlAttribute(AttributeName = "address")]
        public string Address { get; set; }

        [XmlAttribute(AttributeName = "binding")]
        public string Binding { get; set; }

        [XmlAttribute(AttributeName = "contract")]
        public string Contract { get; set; }

        private long _MaxReceivedMessageSize = 6553600;

        [XmlAttribute(AttributeName = "maxReceivedMessageSize")]
        public long MaxReceivedMessageSize
        {
            get { return _MaxReceivedMessageSize; }
            set { _MaxReceivedMessageSize = value; }
        }

        private int _MaxStringContentLength = 819200;

        [XmlAttribute(AttributeName = "maxStringContentLength")]
        public int MaxStringContentLength
        {
            get { return _MaxStringContentLength; }
            set { _MaxStringContentLength = value; }
        }

        #region OpenTimeout
        private string _OpenTimeoutString = "00:00:05";

        [XmlAttribute(AttributeName = "openTimeout")]
        public string OpenTimeoutString
        {
            get { return _OpenTimeoutString; }
            set { _OpenTimeoutString = value; }
        }

        [XmlIgnore]
        public TimeSpan OpenTimeout
        {
            get
            {
                try
                {
                    string[] timeNum = OpenTimeoutString.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                    return new TimeSpan(int.Parse(timeNum[0]), int.Parse(timeNum[1]), int.Parse(timeNum[2]));
                }
                catch
                {
                    throw new Exception("服务" + Contract + "openTimeout=\"" + OpenTimeoutString +
                                        "\"格式错误，请按以下格式配置openTimeout=\"hh:mm:ss\"");
                }
            }
        }
        #endregion

        #region CloseTimeout
        private string _CloseTimeoutString = "00:00:05";

        [XmlAttribute(AttributeName = "closeTimeout")]
        public string CloseTimeoutString
        {
            get { return _CloseTimeoutString; }
            set { _CloseTimeoutString = value; }
        }

        [XmlIgnore]
        public TimeSpan CloseTimeout
        {
            get
            {
                try
                {
                    string[] timeNum = CloseTimeoutString.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                    return new TimeSpan(int.Parse(timeNum[0]), int.Parse(timeNum[1]), int.Parse(timeNum[2]));
                }
                catch
                {
                    throw new Exception("服务" + Contract + "closeTimeout=\"" + CloseTimeoutString +
                                        "\"格式错误，请按以下格式配置closeTimeout=\"hh:mm:ss\"");
                }
            }
        }
        #endregion

        #region ReceiveTimeout
        private string _ReceiveTimeoutString = "00:00:05";

        [XmlAttribute(AttributeName = "receiveTimeout")]
        public string ReceiveTimeoutString
        {
            get { return _ReceiveTimeoutString; }
            set { _ReceiveTimeoutString = value; }
        }

        [XmlIgnore]
        public TimeSpan ReceiveTimeout
        {
            get
            {
                try
                {
                    string[] timeNum = ReceiveTimeoutString.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                    return new TimeSpan(int.Parse(timeNum[0]), int.Parse(timeNum[1]), int.Parse(timeNum[2]));
                }
                catch
                {
                    throw new Exception("服务" + Contract + "receiveTimeout=\"" + ReceiveTimeoutString +
                                        "\"格式错误，请按以下格式配置receiveTimeout=\"hh:mm:ss\"");
                }
            }
        }
        #endregion

        #region SendTimeout
        private string _SendTimeoutString = "00:00:05";

        [XmlAttribute(AttributeName = "sendTimeout")]
        public string SendTimeoutString
        {
            get { return _SendTimeoutString; }
            set { _SendTimeoutString = value; }
        }

        [XmlIgnore]
        public TimeSpan SendTimeout
        {
            get
            {
                try
                {
                    string[] timeNum = SendTimeoutString.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                    return new TimeSpan(int.Parse(timeNum[0]), int.Parse(timeNum[1]), int.Parse(timeNum[2]));
                }
                catch
                {
                    throw new Exception("服务" + Contract + "sendTimeout=\"" + SendTimeoutString +
                                        "\"格式错误，请按以下格式配置sendTimeout=\"hh:mm:ss\"");
                }
            }
        }
        #endregion
    }
}
